/**
 * @projectDescription javascript class for roll call module
 * copyright (c) MUNS, 2010
 * @author xkx
 */
MUN.module.rollcall = function (parent,nations,type)
		{
			this.nations = nations;

			this.nations_table = $("nations-table");
			for(var i=0;i<nations.length;i++)
			{
				var tr =  document.createElement("tr");
				tr.set("id",escape(this.nations[i]));
				tr.set("style","border:solid 1px #7da2ce");
				tr.innerHTML = "<td>"+"<img id='tf' src='"+MUN.util.emptypath+"'/><img src='"+MUN.util.nationflagspath[nations[i]]+"' class='p'/>"+"<span>"+nations[i]+"</span>"+"</td>";
				this.nations_table.grab(tr);
			}
			$("nationimg").src = MUN.util.nationflagspath['empty'];
			$("nationname").innerText = 'Nation Name';
			this.callonnum = 0;
			this.presentnum = 0;
			this.absentnum = 0;
			this.is_reachq = 'No';
			this.n20 = 0;
			this.n50 = 0;
			this.n66 = 0;
			this.present_nations = [];
			this.absent_nations = [];
			this.is_end = false;
			this.tablescroll = new Fx.Scroll('nations-area');
			this.rollcall_buttons = $('rollcall-buttons');
			
			this.status_element_ids = new Array('rollcall-status-area');
			
			this.end_button_area = new Element('div',{'id':'end-button-area','style':"text-align:center;"});
			this.end_button = new Element('button',{'text':'End Roll Call','id':"end-button"}).inject(this.end_button_area);
			this.rollcall_area = $("rollcall-area").inject(parent);
			
			//functions
			this.getnationrow = function(nationname)
				{
					var t = $(escape(nationname));
					if(Browser.safari||Browser.chrome) t = t.firstChild;
					return t;
				};
			this.get_call_on_nation = function()
				{
					return this.nations[this.callonnum];
				};
			this.rcall = function(nationname)
				{   
					 if (!nationname) {
					 	this.is_end = true;
					 	$('nationname').set({'text': ""	});
					 	$('nationimg').src = MUN.util.emptypath;
					 	this.end_button_area.replaces(this.rollcall_buttons);
						this.end_button.addEvent("click", this.end.bind(this));
					 }
					 else {
					 	if (this.is_end) {
					 		this.is_end = false;
					 		this.rollcall_buttons.replaces(this.end_button_area);
					 	}
						$("nationimg").src = MUN.util.nationflagspath[nationname];
						$("nationname").innerText = nationname;
						var t = this.getnationrow(nationname);
						t.addClass('nation-callon');
						this.tablescroll.toElement(escape(nationname));
						}
				};
			this.present = function()
				{
					this.present_nations.push(this.get_call_on_nation());
					this.afterone(this.get_call_on_nation(),1);
				};
			this.absent = function ()
				{
					this.absent_nations.push(this.get_call_on_nation());
					this.afterone(this.get_call_on_nation(),0);
				};
			this.afterone = function (nationname,tf)
				{
					var t = this.getnationrow(nationname);
					t.removeClass('nation-callon');
					t.firstChild.src = (tf==1)? MUN.util.tickpath:MUN.util.crosspath;
					t.getElement("span").style.color = (tf==1)? "green":"red";
					this.presentnum += tf;
					
					this.update_status();
					
					$("message").style.visibility = "visible";
					$("lastnimg").src = MUN.util.nationflagspath[nationname];
					$("lastnname").innerHTML = nationname;
					$("lasttf").innerHTML = (tf==1)? "present":"absent";
					
					this.callonnum++;
					this.rcall(this.get_call_on_nation());
				};
			this.calc = function ()
				{
					this.absentnum = this.nations.length - this.presentnum;
					if(this.presentnum>=(this.nations.length/3)){this.is_reachq = "Yes";}else{this.is_reachq = "No";}
					this.n20 = Math.ceil(this.presentnum*0.2);
					this.n50 = Math.ceil(Math.floor(this.presentnum/2+1));
					this.n66 = Math.ceil(this.presentnum*2/3);
				};
			this.update_status = function()
				{
					this.calc();
					for (var i = 0; i<this.status_element_ids.length; i++)
					{
						var temp = $(this.status_element_ids[i]);
						temp.getElementsByClassName('present-num')[0].innerHTML = this.presentnum;
						temp.getElementsByClassName('absent-num')[0].innerHTML = this.absentnum;
						temp.getElementsByClassName('is-reachq')[0].innerHTML = this.is_reachq;
						temp.getElementsByClassName('n20')[0].innerHTML = this.n20;
						temp.getElementsByClassName('n50')[0].innerHTML = this.n50;
						temp.getElementsByClassName('n66')[0].innerHTML = this.n66;
					}
				};
			this.add_status_element = function(elementname)
				{
					this.status_element_ids.push(elementname);
				};
			this.cancellast = function ()
					{
					$("message").style.visibility = "hidden";
					if (!this.is_end) {
						var t = this.getnationrow(this.get_call_on_nation());//th
						t.removeClass('nation-callon');
					}
					this.callonnum--;
					if (this.present_nations.indexOf(this.get_call_on_nation()) != -1)
					{
						this.presentnum--;
						this.present_nations.erase(this.get_call_on_nation());
					}
					else
					{
						this.absent_nations.erase(this.get_call_on_nation());
					}
					this.update_status();
					t = this.getnationrow(this.get_call_on_nation());
					t.firstChild.src = MUN.util.emptypath;
					t.getElement("span").style.color = "black";
					
					this.rcall(this.get_call_on_nation());
				};
			this.recall = function ()
				{
					$("message").style.visibility = "hidden";
					if (!this.is_end) {;
						t = this.getnationrow(this.get_call_on_nation());
						t.removeClass('nation-callon');
					}
					this.present_nations = [];this.absent_nations = [];
					this.callonnum = 0;
					this.presentnum = 0;
					this.update_status();
					var children = $("nations-table").getChildren();
					for(var i in children)
						{
							var tr = $(children[i]);
							if(tr)
								{
									tr.getElement("img").src = MUN.util.emptypath;
									tr.getElement("span").style.color = "black";
								}
						}
					this.rcall(this.nations[0]);
				};
			this.end = function()
				{
					this.end_button.disabled = "disabled";
					this.rollcall_area.style.display = 'none';
					$('message').style.display = 'none';
					var temp = new Element('div').inject(parent);
					temp.innerHTML = "<p style='font-size:20px;'>Roll Call Ended.<br/>Please send page to Chair to be added into present list.</p><hr/>";
					var temp_text = "<p><b>Numbers of Present Nations: </b>" + this.presentnum + "</p>";
					temp_text += "<p><b>Numbers of Abcent Nations: </b>" + this.absentnum + "</p>";
					temp_text += "<p><b>Reach Quorum: </b>" + this.is_reachq + "</p>";
					temp_text += "<p><b>20% of Nations: </b>"+ this.n20+ "</p>";
					temp_text += "<p><b>Simple Majority: </b>"+ this.n50+ "</p>";
					temp_text += "<p><b>2/3 Majority: </b>"+ this.n66+ "</p>";
					temp_text += "<p><b>Present Nations: </b>"+ this.present_nations.join(', ')+ "</p>";
					temp_text += "<p><b>Absent Nations: </b>"+ this.absent_nations.join(', ')+ "</p>";
					new Element('p').inject(temp).set({
						'styles':{'border':'2.5px solid #A1C4DF'},
						'html':temp_text
						});
					conference_data.presentnum = this.presentnum;
					conference_data.absentnum = this.absentnum;
					conference_data.is_reachq = this.reachq;
					conference_data.n20 = this.n20;
					conference_data.n20 = this.n50;
					conference_data.n66 = this.n66;
					conference_data.present_nations = this.present_nations;
					conference_data.absent_nations = this.absent_nations;
					
					MUN.status.status_update('out-of-rollcall');
				
				};
			this.init = function()
				{
					this.rollcall_area.set({'style':"visibility:visible;"});
					$("present").addEvent("click",this.present.bind(this));
					$("absent").addEvent("click",this.absent.bind(this));
					$("cancellast").addEvent("click",this.cancellast.bind(this));
					$("recall").addEvent("click",this.recall.bind(this));
					this.rcall(this.get_call_on_nation());
					return;
				}
			return true;
		};